package com.ca.ridersclubapplication.service;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import com.ca.ridersclubapplication.data.DataHandler;
import com.ca.ridersclubapplication.data.entity.BooleanEntity;
import com.ca.ridersclubapplication.request.AfterExecutor;
import com.ca.ridersclubapplication.request.MessageRequester;
import com.ca.ridersclubapplication.request.dto.MsgListDto;
import com.ca.ridersclubapplication.request.dto.Result;
import com.ca.ridersclubapplication.util.Logger;

import java.util.ArrayList;
import java.util.List;

public class MessageService extends Service {

    private Intent intent;

    public static int unReadCount = 0;
    public static List<MsgListDto> list = new ArrayList<>();
    public static Activity activity;

    private Handler getListHandler;
    private Runnable getListRunnable;
    private final Logger log = Logger.getInstance("MessageService");

    @Override
    public void onCreate() {

        log.e("消息服务启动...");

        getListHandler = new Handler();

        getListRunnable = () -> {

            if (!DataHandler.isLogin(activity)) {
                getListHandler.removeCallbacks(getListRunnable);
                log.e("停止MessageService任务");
                return;
            }

            MessageRequester.listMsgList(activity, new AfterExecutor<List<MsgListDto>>() {
                @Override
                public void execute(Result<List<MsgListDto>> result) {
                    List<MsgListDto> data = result.getData();
                    if (result.isSuccess() && data != null) {
                        //  请求成功，添加数据
                        unReadCount = 0;
                        boolean ok= true;
                        for (MsgListDto dto : data) {
                            if (dto != null) {
                                Long count = dto.getUnReadCount();
                                MessageService.unReadCount += count == null ? 0L : count;
                                if (!list.contains(dto)) {
                                    ok = false;
                                }
                            }
                        }

                        if (!ok) {
                            list = data;
                            //  放入数据处理器中，通知回调组件更新数据
                            DataHandler.putData(new BooleanEntity("HasNewMsg", true));
                            log.e("有新消息：size = " + data.size());
                        }
                    }

                    getListHandler.postDelayed(getListRunnable, 500);
                }
            });
        };
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        this.intent = intent;
        getListHandler.post(getListRunnable);

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        getListHandler.removeCallbacks(getListRunnable);
        log.e("停止MessageService");
    }

    public static synchronized void setUnReadCount(int uc) {
        unReadCount = uc;
        DataHandler.putData(new BooleanEntity("HasNewMsg", true));
    }
}